package com.wannengbang.storemobile.merchant;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.BaseActivity;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.BaseResponseBean;
import com.wannengbang.storemobile.bean.RechargeConfigBean;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;
import com.wannengbang.storemobile.merchant.adapter.ReConfigListAdapter;
import com.wannengbang.storemobile.utils.FastClickUtils;
import com.wannengbang.storemobile.utils.NumberFormatUtils;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.AppTitleBar;
import com.wannengbang.storemobile.widget.ViewLoading;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MerchantRechargeConfigActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_add_project)
    TextView tvAddProject;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IHomePageModel homePageModel;
    private ReConfigListAdapter listAdapter;
    private List<RechargeConfigBean.DataBean> beanList;
    private SkeletonScreen skeletonScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_recharge_config);
        ButterKnife.bind(this);
        initData();
    }
    public void initData() {
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new ReConfigListAdapter(beanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {
            if (FastClickUtils.isFastClick()) {
                return;
            }
            if (type == 1) {
                beanList.remove(position);
                listAdapter.notifyDataSetChanged();
            }
        });

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_income_list)
                .show();

        requestData();
    }

    public void requestData() {
        homePageModel.requestMerchantRechargeConfig(new DataCallBack<RechargeConfigBean>() {
            @Override
            public void onSuccessful(RechargeConfigBean bean) {
                beanList.clear();
                if (bean.getData() != null && bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                }
                for (RechargeConfigBean.DataBean dataBean : beanList) {
                    if (!TextUtils.isEmpty(dataBean.getRecharge_money())) {
                        dataBean.setRecharge_money(NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getRecharge_money()) / 100));
                    }
                    if (!TextUtils.isEmpty(dataBean.getSend_money())) {
                        dataBean.setSend_money(NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getSend_money()) / 100));
                    }
                }
                listAdapter.notifyDataSetChanged();
                skeletonScreen.hide();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_add_project, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_project:
                beanList.add(new RechargeConfigBean.DataBean());
                listAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_commit:
                if (beanList.size() == 0) {
                    ToastUtil.showShort("请添加配置");
                    return;
                }
                List<RechargeConfigBean.DataBean> dataBeanList = new ArrayList<>();
                for (RechargeConfigBean.DataBean dataBean : beanList) {

                    RechargeConfigBean.DataBean bean = new RechargeConfigBean.DataBean();
                    if (!TextUtils.isEmpty(dataBean.getSend_money())) {
                        bean.setSend_money(NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getSend_money()) * 100));
                    }
                    if (!TextUtils.isEmpty(dataBean.getRecharge_money())) {
                        bean.setRecharge_money(NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getRecharge_money()) * 100));
                    }
                    bean.setCreate_time(dataBean.getCreate_time());
                    bean.setDelete_time(dataBean.getDelete_time());
                    bean.setId(dataBean.getId());
                    bean.setStore_no(dataBean.getStore_no());

                    dataBeanList.add(bean);
                }
                requestStoreSaveRechargeConfig(dataBeanList);
                break;
        }
    }

    public void requestStoreSaveRechargeConfig(List<RechargeConfigBean.DataBean> beanList) {
        ViewLoading.showProgress(mActivity, "保存中......");
        homePageModel.requestMerchantSaveRechargeConfig(beanList, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("保存成功");
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
